package com.longrise.android.by.javascriptInterface;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.longrise.android.byjk.model.ShareParams;
import com.longrise.android.by.activity.BYWebActivity;

/**
 * Created by godliness on 2017/3/20.
 */

public class XUCPInterface {

    private BYWebActivity mCxt;

    public XUCPInterface(BYWebActivity activity) {
        this.mCxt = activity;
    }

    @JavascriptInterface
    public void shareParams(String title, String content, String url, String iconUrl) {
        if(mCxt != null){
            ShareParams shareParams = new ShareParams();
            shareParams.mTitle = title;
            shareParams.mContent = content;
            shareParams.mUrl = url;
            shareParams.mHeadUrl = iconUrl;
            mCxt.showShareDialog(shareParams);
        }
    }

    @JavascriptInterface
    public void confirm(final String url) {
        if (!TextUtils.isEmpty(url)) {
            mCxt.mHandler.post(new Runnable() {
                @Override
                public void run() {
                    linkTo(url);
                }
            });
        }
    }

    @JavascriptInterface
    public void loadFinish(){
        mCxt.mHandler.post(new Runnable() {
            @Override
            public void run() {
                mCxt.isLoading(false);
            }
        });
    }

    private void linkTo(String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri linkUrl = Uri.parse(url);
        intent.setData(linkUrl);
        mCxt.startActivity(intent);
    }
}
